/**
 * 
 */
package leetCode;

/**
 * @author zhong
 *
 */
public class LongestTurbulentSubarray {
	public int maxTurbulenceSize(int[] A) {
		int n = A.length;
		if (n < 3) {
			return n;
		}
		int[] B = new int[n - 1];
		for (int i = 0; i < n - 1; i++) {
			if (A[i] < A[i + 1]) {
				B[i] = 1;
			} else if (A[i] > A[i + 1]) {
				B[i] = -1;
			}
		}
		int curLen = 0;
		int before = B[0];
		int maxRes = 0;
		for (int i = 1; i < B.length; i++) {
			if (B[i] == -before) {
				curLen++;
			} else {
				maxRes = Math.max(maxRes, curLen);
				curLen = 0;
			}
			before = B[i];
		}
		maxRes = Math.max(maxRes, curLen);
		return maxRes + 2;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int[] A = { 4 };// { 9, 4, 2, 10, 7, 8, 8, 1, 9 };
		System.out.println(new LongestTurbulentSubarray().maxTurbulenceSize(A));
	}

}
